package com.launch.carmanager.auth;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.launch.carmanager.R;
import com.launch.carmanager.common.base.BaseActivity;
import com.launch.carmanager.common.utils.ResourceUtils;
import com.launch.carmanager.common.utils.TimerTaskUtils;
import com.launch.carmanager.common.widget.TitleBar;
import com.launch.carmanager.lanuncher.LauncherActivity;
import com.zyyoona7.popup.EasyPopup;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ganquan on 2019/3/28 14:33
 */
public class AuthActivity extends BaseActivity<AuthPresenter> implements AuthContract.View {


    @BindView(R.id.image_back)
    ImageView imageBack;
    @BindView(R.id.country_code)
    EditText countryCode;
    @BindView(R.id.view)
    View view;
    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.delete_input)
    ImageView deleteInput;
    @BindView(R.id.etxtCheckCode)
    EditText etxtCheckCode;
    @BindView(R.id.btnReSend)
    TextView btnReSend;
    @BindView(R.id.edit_verfi_code)
    EditText editVerfiCode;
    @BindView(R.id.ll_verficode)
    LinearLayout llVerficode;
    @BindView(R.id.view1)
    View view1;
    @BindView(R.id.check_box)
    CheckBox checkBox;
    @BindView(R.id.text_read)
    TextView textRead;
    @BindView(R.id.text_agreement)
    TextView textAgreement;
    @BindView(R.id.text_and)
    TextView textAnd;
    @BindView(R.id.text_privacy)
    TextView textPrivacy;
    @BindView(R.id.button_auth)
    Button buttonAuth;
    @BindView(R.id.change_path)
    LinearLayout changePath;

    private boolean proIsChecked = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auth);
        ButterKnife.bind(this);

        phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                deleteInput.setVisibility(View.VISIBLE);
                if (s.length() > 10) {
                    btnReSend.setTextColor(ResourceUtils.getColor(AuthActivity.this, R.color.text_green));
                    btnReSend.setClickable(true);
                } else {
                    btnReSend.setTextColor(ResourceUtils.getColor(AuthActivity.this, R.color.text_light_gray));
                    btnReSend.setClickable(false);
                }
            }
        });
        etxtCheckCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (proIsChecked && phone.getText().toString().trim().length() > 10 && s.length() > 3) {
                    buttonAuth.setBackground(ResourceUtils.getDrawable(AuthActivity.this, R.drawable.button_white_green));

                }
            }
        });
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                proIsChecked = isChecked;
                if (isChecked && phone.getText().toString().trim().length() > 10 && etxtCheckCode.getText().toString().length() > 3) {
                    buttonAuth.setBackground(ResourceUtils.getDrawable(AuthActivity.this, R.drawable.button_white_green));

                }
            }
        });
    }


    @Override
    protected AuthPresenter newPresenter() {
        return new AuthPresenter(this);
    }

    private long clickCurrentTime = 0;

    @OnClick({R.id.btnReSend, R.id.button_auth, R.id.delete_input})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btnReSend:
                if ((System.currentTimeMillis() - clickCurrentTime) < 4000) return;
                clickCurrentTime = System.currentTimeMillis();
                if (!TextUtils.isEmpty(phone.getText().toString().trim()) && phone.getText().toString().length() == 11) {

                    String phoneNo = phone.getText().toString().trim();
                    showProgressDialog("加载中...");
                    mPresenter.sendCode(phoneNo);
                } else {
                    ToastUtils.showShort("请输入正确的手机号码");
                }
                break;
            case R.id.delete_input:
                buttonAuth.setBackground(ResourceUtils.getDrawable(AuthActivity.this, R.drawable.button_white_green));
                phone.setText("");
                deleteInput.setVisibility(View.INVISIBLE);
                btnReSend.setTextColor(ResourceUtils.getColor(AuthActivity.this, R.color.text_light_gray));
                break;
            case R.id.button_auth:
                showProgressDialog("加载中");
                mPresenter.auth("15807990881", "2", "3");
                break;
        }
    }

    @Override
    public void authSuccess() {
        dismissProgressDialog();
        openPage(LauncherActivity.class);
    }

    @Override
    public void sendCodeSuccess() {
        dismissProgressDialog();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                startTimer();
                btnReSend.setEnabled(false);
            }
        });

    }

    private void stopTimer() {
        TimerTaskUtils.stopTimer(AuthActivity.class.getSimpleName());
    }

    private int seconds = 60;

    private void startTimer() {

        TimerTaskUtils.startTimer(AuthActivity.class.getSimpleName(), 1000, new Runnable() {
            @Override
            public void run() {
                seconds--;
                if (seconds < 1) {
                    seconds = 60;
                    btnReSend.setEnabled(true);
                    btnReSend.setText(getString(R.string.re_send_code));
                    editVerfiCode.setFocusable(true);
                    editVerfiCode.setFocusableInTouchMode(true);
                    stopTimer();
                } else {
                    btnReSend.setText(String.valueOf(seconds) + "s");
                }
            }
        }, handler);
        ToastUtils.showShort("验证码发送成功");
    }

    Handler handler = new Handler(Looper.getMainLooper());

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }

    }
}
